package cn.qayy.dto;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.ServletContext;

import cn.qayy.constants.CollectionConstant;
import cn.qayy.constants.Constant;
import cn.qayy.service.Uploadable;
import cn.qayy.utils.ServletContextUtils;
import cn.qayy.utils.StringUtil;

/**
 * 
 * 
 * @author wangyu
 * 好伙伴DTO
 * @date 2018-09-03
 */
public class ClientDTO extends commonDTO implements Serializable,Uploadable{
	/***
	 * 验证码，如果不提供身份证就是UUID
	 */
    private String uuid="";
    /**
     * 姓名
     */
    private String name;

    /**
     * 手机号
     */
    private String telephone;

    /**
     * 省份
     */
    private String province;

    /**
     * 地市
     */
    private String city;

    /**
     * 区县
     */
    private String country;

    /**
     * 入货乙方
     */
    private String terminalCollection;
    
    /**
     * 用户类型
     */
    private String type;
    
    /**
	 * 修订标识
	 */
	private String isUpdate = "E";
	
	private String id;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name == null ? null : name.trim();
    }

    public String getTelephone() {
        return telephone;
    }

    public void setTelephone(String telephone) {
        this.telephone = telephone == null ? null : telephone.trim();
    }

    public String getProvince() {
        return province;
    }

    public void setProvince(String province) {
        this.province = province == null ? null : province.trim();
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city == null ? null : city.trim();
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country == null ? null : country.trim();
    }

    public String getTerminalCollection() {
        return terminalCollection;
    }

    public void setTerminalCollection(String terminalCollection) {
        this.terminalCollection = terminalCollection == null ? null : terminalCollection.trim();
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type == null ? null : type.trim();
    }
    
	public String getIsUpdate() {
		return isUpdate;
	}

	public void setIsUpdate(String isUpdate) {
		this.isUpdate = isUpdate;
	}

	public ClientDTO(String id, String name, String telephone, String province, String city, String country,
			String terminalCollection, String type) {
		super();
		this.id = id;
		this.name = name;
		this.telephone = telephone;
		this.province = province;
		this.city = city;
		this.country = country;
		this.terminalCollection = terminalCollection;
		this.type = type;
	}

	public ClientDTO() {
		super();
	}

	@Override
	public Uploadable filter(Map<String, List<Uploadable>> map, Set<Uploadable> set, int num) throws Exception {
		/**容纳错误消息的list*/
 		List<Uploadable> mlist = new ArrayList<Uploadable>(Constant.SMALL_CAPACITY);
 		/**容纳正确的，最终用于保存到数据库的数据**/
 		List<Uploadable> list = new ArrayList<Uploadable>(Constant.SMALL_CAPACITY);
 	    
 		/***
 		 * 过滤掉所有的空格
 		 */
 		
 		String uuid = StringUtil.trimAll(this.getUuid());  
 		String name = StringUtil.trimAll(this.getName());  
 		String telephone = StringUtil.trimAll(this.getTelephone()); 
 		String province = StringUtil.trimAll(this.getProvince());      
 		String city = StringUtil.trimAll(this.getCity());    
 		String country = StringUtil.trimAll(this.getCountry());   
 		String terminalCollection = StringUtil.trimAll(this.getTerminalCollection());      
 		String type = StringUtil.trimAll(this.getType());       
 		String id = StringUtil.trimAll(this.getId()); 
 		
 		/**
 	     * 判断是否导入的模板是正确的模板
 	     */
 	    if(num == 1) {
 	    		   if(
 	    			!CollectionConstant.client_mb.get(0).contains(uuid)    
 	    			||!CollectionConstant.client_mb.get(1).contains(name)  
 	    			||!CollectionConstant.client_mb.get(2).contains(telephone)    
 	    			||!CollectionConstant.client_mb.get(3).contains(province)      
 	    			||!CollectionConstant.client_mb.get(4).contains(city)      
 	    			||!CollectionConstant.client_mb.get(5).contains(country)       
 	    			||!CollectionConstant.client_mb.get(6).contains(terminalCollection)      
 	    			||!CollectionConstant.client_mb.get(7).contains(type)       
 		    		||!CollectionConstant.client_mb.get(8).contains(isUpdate)  
 		    		||!CollectionConstant.client_mb.get(9).contains(id)  
 	    			) {
 	    		MsgInfo msg = new MsgInfo(Constant.ERROR_MB,Constant.ERROR_MB,Constant.ERROR_MB,Constant.ERROR_NUM+num);
 				mlist.add(msg);
 				map.put("stop", mlist);
 		    	return null;
 		    }
 	    } else {
 	    	/***
 			 * 获取项目上下文的共享数据
 			 */
 			ServletContext servletContext = ServletContextUtils.getServletContext();
 			//入货乙方
 			HashSet<String> cNames = ((HashSet<String>)servletContext.getAttribute("cNames"));
 			/**省份**/
			Set<String> provinces = (HashSet<String>) servletContext.getAttribute("provinces");
			/**地市**/
			Set<String> citys = (HashSet<String>) servletContext.getAttribute("citys");
			/**客户身份验证**/
			Set<String> uuidList = (HashSet<String>) servletContext.getAttribute("uuidList");
 	    	/***
 		     * 数据合格性校验
 		     */
 			if(!CollectionConstant.types.contains(type)) {
 				MsgInfo msg = new MsgInfo(type,"用户类型",CollectionConstant.types.toString(),Constant.ERROR_NUM+num);
 				mlist.add(msg);
 			}
 			
 			if(!CollectionConstant.cudFlags.contains(isUpdate)) {
 				MsgInfo msg = new MsgInfo(isUpdate,"修订标识",CollectionConstant.cudFlags.toString(),Constant.ERROR_NUM+num);
 				mlist.add(msg);
 			}
 			if("E".equals(isUpdate)) {
				if(StringUtil.isBlank(id)) {
					MsgInfo msg = new MsgInfo(isUpdate,"修订标识有误或id有误","修订标识为E或空白时，id不能为空。id为空白时，修订标识只能是N",Constant.ERROR_NUM+num);
					mlist.add(msg);
				}
			}
 			if(isUpdate.equals("N")) {
 				/***
 	 			 * 判断uuid是否存在客户表中
 	 			 */
 	 			if(uuidList.contains(uuid)) {
 					MsgInfo msg = new MsgInfo(uuid,"身份证",Constant.EXIST_UUID,Constant.ERROR_NUM+num);
 					mlist.add(msg);
 				}
 			} 
 			
 			/***
 			 * 判断是否在注册库中注册
 			 */
 			if(!terminalCollection.trim().equals("")&&!cNames.contains(terminalCollection)) {
				MsgInfo msg = new MsgInfo(terminalCollection,"入货乙方",Constant.NO_REGISTER,Constant.ERROR_NUM+num);
				mlist.add(msg);
			}
 			/***
			 * 省份参考【基础库】的省份
			 */
			if (!provinces.contains(province)&&!province.trim().equals("")) {
				MsgInfo msg = new MsgInfo(province, "省份", Constant.BASIC_DATA, Constant.ERROR_NUM + num);
				mlist.add(msg);
			}
			/**
			 * 参考【基础库】的地市
			 */
			if (!citys.contains(city)&&!city.trim().equals("")) {
				MsgInfo msg = new MsgInfo(city, "地市", Constant.BASIC_DATA, Constant.ERROR_NUM + num);
				mlist.add(msg);
			}
 			/***
 			 * 返回处理后的结果数据
 			 */
 			if(!mlist.isEmpty()) {
 				map.put("fail"+num, mlist);
 			} 
 			/***把验证通过的合格数据返回*/
 			this.setAll(id,uuid,name,telephone,province,city,country,terminalCollection,type,isUpdate);
 			//不是空行才添加
			if(StringUtil.isNotBlank(name)) {
				set.add(this);
			}
 			return this;
 	    }
	    return null;
	}

	@Override
	public String flag() throws Exception {
		return this.isUpdate;
	}

	@Override
	public String toString() {
		return "ClientDTO [id=" + id + ", name=" + name + ", telephone=" + telephone + ", province=" + province
				+ ", city=" + city + ", country=" + country + ", terminalCollection=" + terminalCollection + ", type=" + type + ", isUpdate=" + isUpdate + "]";
	}
	
	public void setAll(String id,String uuid, String name, String telephone, String province, String city, String country,
			String terminalCollection, String type, String isUpdate) {
		this.id = id;
		this.uuid = uuid;
		this.name = name;
		this.telephone = telephone;
		this.province = province;
		this.city = city;
		this.country = country;
		this.terminalCollection = terminalCollection;
		this.type = type;
		this.isUpdate = isUpdate;
	}

	public ClientDTO(String id, String name, String telephone, String province, String city, String country,
			String terminalCollection, String type, String isUpdate) {
		super();
		this.id = id;
		this.name = name;
		this.telephone = telephone;
		this.province = province;
		this.city = city;
		this.country = country;
		this.terminalCollection = terminalCollection;
		this.type = type;
		this.isUpdate = isUpdate;
	}

	public ClientDTO(Integer pageNumber, Integer pageSize) {
	}

	public String getUuid() {
		return uuid;
	}

	public void setUuid(String uuid) {
		this.uuid = uuid;
	}
}